/********************************** (C) COPYRIGHT *******************************
 * File Name          : ch32v20x_it.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2021/06/06
 * Description        : Main Interrupt Service Routines.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for 
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "ch32v20x_it.h"
#include "eth_driver.h"
#include "usart.h"

volatile u8 tim1_flag = 0;
volatile u16 polling_count = 0;


void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void ETH_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM1_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

/*********************************************************************
 * @fn      NMI_Handler
 *
 * @brief   This function handles NMI exception.
 *
 * @return  none
 */
void NMI_Handler(void)
{
    /**/
}

/*********************************************************************
 * @fn      HardFault_Handler
 *
 * @brief   This function handles Hard Fault exception.
 *
 * @return  none
 */
void HardFault_Handler(void)
{
    printf("HardFault_Handler\r\n");
    printf("mcause:%08x\r\n",__get_MCAUSE());
    printf("mtval:%08x\r\n",__get_MTVAL());
    printf("mepc:%08x\r\n",__get_MEPC());
    NVIC_SystemReset();
    while (1);
}

/*********************************************************************
 * @fn      ETH_IRQHandler
 *
 * @brief   This function handles ETH exception.
 *
 * @return  none
 */
void ETH_IRQHandler(void)
{
    WCHNET_ETHIsr();
}

/*********************************************************************
 * @fn      TIM2_IRQHandler
 *
 * @brief   This function handles TIM2 exception.
 *
 * @return  none
 */


void TIM1_UP_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM1, TIM_IT_Update) == SET) {
        polling_count++;
        tim1_flag = 1;  // 仅设置标志
    }
    TIM_ClearITPendingBit(TIM1, TIM_IT_Update);
}


void TIM2_IRQHandler(void)
{
    WCHNET_TimeIsr(WCHNETTIMERPERIOD);
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}


/*该定时用作超时判断，如果超时则说明接收完一个报文了,en_handle_usart去结束串口接收使得其它程序处理串口*/
 void TIM3_IRQHandler(void)  //��ʱ�����ȥ����RTU,˳��ı���ѯʱ�����RTU_ReadyToProcess ����ʱΪ1ms
 {
     if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) {
       // ��鶨ʱ���Ƿ�ʱ
      
        Timeout_count++;
        
         // �����ʱ�ˣ����� 3.5 ���ַ���ʱ�䣩��ֹͣ����
         if (Timeout_count >= 6) {
           En_handle_usart();  // ֹͣ����
             Timeout_count = 0;

         }
         
        
        
     }
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);  // ����жϱ�־
     
 }
